package com.aps.util;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author liaoguangan
 * @description <>
 * @date 2018/4/4 13:48
 */
public class JdbcUtil {

    /**
     * 实体字段转set方法
     *
     * @param field
     * @return
     */
    public static String getSetMethodByField(String field) {
        StringBuilder sb = new StringBuilder("set");
        for (int i = 0; i < field.length(); i++) {
            if (i == 0) {
                sb.append(String.valueOf(field.charAt(i)).toUpperCase());
                continue;
            }
            sb.append(field.charAt(i));
        }
        return sb.toString();
    }

    /**
     * 数据库字段转方法名
     *
     * @param field
     * @return
     */
    public static String getSetMethodByDbField(String field) {
        StringBuilder sb = new StringBuilder("set");
        for (int i = 0; i < field.length(); i++) {
            if (i == 0) {
                sb.append(String.valueOf(field.charAt(i)).toUpperCase());
                continue;
            }
            if ("_".equals(String.valueOf(field.charAt(i)))) {
                sb.append(String.valueOf(field.charAt(i + 1)).toUpperCase());
                i++;
                continue;
            } else {
                sb.append(field.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * 方法名转数据库字段
     *
     * @param field
     * @return
     */
    public static String getDbFieldName(String field) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < field.length(); i++) {
            if (Character.isUpperCase(field.charAt(i))) {
                sb.append("_");
            }
            sb.append(String.valueOf(field.charAt(i)).toLowerCase());
        }
        return sb.toString();
    }

    /**
     * 获取包括父类的所有属性
     *
     * @param clazz
     * @param fields
     * @return
     */
    public static Field[] getAllFields(Class clazz, List<Field> fields) {
        Field[] fds = clazz.getDeclaredFields();
        for (Field fd : fds) {
            if ("objectMapper".equals(fd.getName())) continue;
            fields.add(fd);
        }
        if (clazz.getSuperclass() != null) {
            return getAllFields(clazz.getSuperclass(), fields);
        }

        return listToArray(fields);
    }

    /**
     * @param fields
     * @return
     */
    private static Field[] listToArray(List<Field> fields) {
        Field[] fds = new Field[fields.size()];
        return fields.toArray(fds);
    }

    /**
     * 判断数据集中是否包含某字段
     *
     * @param rs
     * @param column
     * @return
     */
    public static Boolean hasColumn(ResultSet rs, String column) {
        try {
            if (rs.findColumn(column) != -1) {
                return true;
            } else return false;
        } catch (SQLException e) {
            return false;
        }
    }

    /**
     * 获取枚举值
     *
     * @param str
     * @param fd
     * @return
     */
    public static Object str2Enum(String str, Field fd) {
        Object[] objs = fd.getType().getEnumConstants();
        for (Object obj : objs) {
            if (obj.toString().equals(str)) {
                return obj;
            }
        }
        return null;
    }
}
